/*
 * support3
 */
package com.pitop.frontend.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.FrontendI18nCategoryException;
import com.pitop.core.tools.StringTools;
import com.pitop.frontend.dao.FrontendI18nCategoryDAO;
import com.pitop.frontend.entity.FrontendI18nCategory;
import com.pitop.frontend.entity.FrontendI18nCategoryState;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("frontendI18nCategorySV")
@Slf4j
public class FrontendI18nCategorySVImpl extends BaseSVImpl<FrontendI18nCategory, Long> implements FrontendI18nCategorySV {

    @Autowired
    private FrontendI18nCategoryDAO frontendI18nCategoryDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return frontendI18nCategoryDAO;
    }

    /**
     * 保存前端国际化分类对象
     * 1.参数合法性验证
     * ****非空判断
     * ****国际化分类名是否存在
     * ****国际化分类前缀是否存在,及前缀是否是英文
     * 2.保存数据
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(FrontendI18nCategory entity) throws BaseException {
        //1.参数合法性验证，非空判断
        if (entity == null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(entity.getName())) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(entity.getPrefix())) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        //国际化分类名是否存在
        Map<String, Object> params = new HashedMap();
        params.put("name", entity.getName());
        FrontendI18nCategory frontendI18nCategory = frontendI18nCategoryDAO.load(params);
        if (frontendI18nCategory != null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Exists);
        }
        //前缀是否是英文
        if (!StringTools.checkEnlish(entity.getPrefix())) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Must_Be_In_English);
        }
        //国际化分类前缀是否存在
        params = new HashedMap();
        params.put("prefix", entity.getPrefix());
        frontendI18nCategory = frontendI18nCategoryDAO.load(params);
        if (frontendI18nCategory != null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Exists);
        }
        //2.保存数据
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        entity.setState(FrontendI18nCategoryState.Enable.name());
        super.save(entity);
    }

    /**
     * 加载一个对象FrontendI18nCategory
     *
     * @param id id* @param code 编码
     * @return FrontendI18nCategory
     */
    @Override
    public FrontendI18nCategory load(Long id, String code) {
        if (id == null && code == null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return frontendI18nCategoryDAO.load(param);
    }

    /**
     * 加载一个对象FrontendI18nCategory 通过id
     *
     * @param id id
     * @return FrontendI18nCategory
     */
    @Override
    public FrontendI18nCategory loadById(Long id) {
        if (id == null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return frontendI18nCategoryDAO.loadById(id);
    }

    /**
     * 加载一个对象FrontendI18nCategory 通过code
     *
     * @param code 编码
     * @return FrontendI18nCategory
     */
    @Override
    public FrontendI18nCategory loadByCode(String code) {
        if (code == null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return frontendI18nCategoryDAO.loadByCode(code);
    }

    /**
     * 加载一个对象FrontendI18nCategory 通过code
     *
     * @param code 编码
     * @return FrontendI18nCategory
     * @Param state 状态
     */

    @Override
    public FrontendI18nCategory load(String code, String state) {
        if(StringTools.isEmpty(code)) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if(StringTools.isEmpty(state)) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Map<String, Object> params = new HashedMap();
        params.put("code", code);
        params.put("state", state);
        return  frontendI18nCategoryDAO.load(params);
    }


    /**
     * 根据主键id,oldStates 共同更新 FrontendI18nCategory 的状态到newState状态
     *
     * @param id        id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, FrontendI18nCategoryState newState, FrontendI18nCategoryState... oldStates) {
        if (id == null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        frontendI18nCategoryDAO.updateStateById(id, new Date(), newState.name(), states);
    }

    /**
     * 根据主键code,oldStates 共同更新 FrontendI18nCategory 的状态到newState状态
     *
     * @param code      编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByCode(String code, FrontendI18nCategoryState newState, FrontendI18nCategoryState... oldStates) {
        if (code == null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        frontendI18nCategoryDAO.updateStateByCode(code, new Date(), newState.name(), states);
    }

    /**
     * 根据主键id 更新 FrontendI18nCategory 的状态到另一个状态
     *
     * @param id    id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, FrontendI18nCategoryState state) {
        if (id == null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        frontendI18nCategoryDAO.updateById(id, state.name(), new Date());
    }

    /**
     * 根据主键code 更新 FrontendI18nCategory 的状态到另一个状态
     *
     * @param code  编码
     * @param state 状态
     */
    @Override
    public void updateByCode(String code, FrontendI18nCategoryState state) {
        if (code == null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        frontendI18nCategoryDAO.updateByCode(code, state.name(), new Date());
    }


    /**
     * 删除对象FrontendI18nCategory
     *
     * @param id id* @param code 编码
     * @return FrontendI18nCategory
     */
    @Override
    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        frontendI18nCategoryDAO.delete(param);
    }


    /**
     * 查询FrontendI18nCategory分页
     *
     * @param frontendI18nCategory 对象
     * @param offset               查询开始行
     * @param limit                查询行数
     * @return List<FrontendI18nCategory>
     */
    @Override
    public List<FrontendI18nCategory> list(FrontendI18nCategory frontendI18nCategory, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (frontendI18nCategory != null) {
            map = JSON.parseObject(JSON.toJSONString(frontendI18nCategory, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return frontendI18nCategoryDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(FrontendI18nCategory frontendI18nCategory) {
        Map<String, Object> map = null;
        if (frontendI18nCategory != null) {
            map = JSON.parseObject(JSON.toJSONString(frontendI18nCategory, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return frontendI18nCategoryDAO.count(map);
    }

    /**
     * 查询FrontendI18nCategory分页
     *
     * @param id     id* @param code  编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<FrontendI18nCategory>
     */
    @Override
    public List<FrontendI18nCategory> list(Long id, String code, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return frontendI18nCategoryDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String code) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return frontendI18nCategoryDAO.count(param);
    }

}
